package com.yl.order.ui.dialog;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.yl.common.ui.BaseDialogFragment;
import com.yl.order.R;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Locale;

/**
 * create by cjx on 2024/5/16
 * desc: 显示网络状态
 */
public class NetInfoDialogFragment extends BaseDialogFragment {

    private boolean serverConnect;


    public static NetInfoDialogFragment newInstance() {
        return new NetInfoDialogFragment();
    }

    @Override
    public int getViewId() {
        return R.layout.dialog_net_info;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Context context = getContext();
        if (context == null) {
            return;
        }
        findViewById(R.id.close).setOnClickListener(v -> dismiss());

        TextView typeView = findViewById(R.id.type);
        typeView.setOnClickListener(v -> {
            Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
            intent.putExtra("only_access_points", true);
            startActivityForResult(intent, 1);
        });
        TextView ipView = findViewById(R.id.ip);
        TextView serverView = findViewById(R.id.server);

        serverView.setSelected(serverConnect);
        if (serverConnect) {
            serverView.setText(R.string.net_connect);
        } else {
            serverView.setText(R.string.net_disconnect);
        }

        ConnectivityManager netManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = netManager.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            typeView.setSelected(true);
            ipView.setSelected(true);
            // wifi
            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                typeView.setText(R.string.net_type_wifi);
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                int ipAddress = wifiInfo.getIpAddress();
                String address = String.format(Locale.CHINA, "%d.%d.%d.%d", ipAddress & 0xFF, ipAddress >> 8 & 0xFF, ipAddress >> 16 & 0xFF, ipAddress >> 24 & 0xFF);
                ipView.setText(address);
            } else {
                typeView.setText(R.string.net_type_ethernet);
                final String interfaceName = "eth0";
                try {
                    Enumeration<NetworkInterface> netInterface = NetworkInterface.getNetworkInterfaces();
                    while (netInterface.hasMoreElements()) {
                        NetworkInterface network = netInterface.nextElement();
                        if (!network.isUp()) {
                            continue;
                        }
                        if (!interfaceName.equals(network.getDisplayName())) {
                            continue;
                        }
                        Enumeration<InetAddress> addressList = network.getInetAddresses();
                        while (addressList.hasMoreElements()) {
                            InetAddress inetAddress = addressList.nextElement();
                            if (inetAddress instanceof Inet4Address) {
                                ipView.setText(inetAddress.getHostAddress());
                            }
                        }
                    }
                } catch (Exception ignore) {
                    ipView.setText("0.0.0.0");
                    ipView.setSelected(false);
                }
            }
        } else {
            ipView.setText("0.0.0.0");
            typeView.setText(R.string.net_disconnect);
        }
    }

    @Override
    protected float getDialogWidth() {
        return 0.4f;
    }

    public NetInfoDialogFragment setServerConnect(boolean serverConnect) {
        this.serverConnect = serverConnect;
        return this;
    }
}
